﻿# More info at http://psautodeploy.codeplex.com
# Author: SPFernando

$LogPath =  "C:\$(gc env:computername).log"

function LogWrite {
	param (
		[parameter(Position=0, Mandatory=$false)]
   		[string]$LogText
   	)
	if ($LogText) {
		$StartDate = Get-Date 
		[string]$Time = "{0:D2}" -f ($StartDate.Hour) + ":" + "{0:D2}" -f ($StartDate.Minute) + ":" + "{0:D2}" -f ($StartDate.Second)
		$LogOperation =  $StartDate.ToShortDateString() + " | " + $Time + " - " + $LogText
	   	Add-Content $LogPath -value $LogOperation
	}
	else {
		Add-Content $LogPath -value "--------------------------------------------------------------------------------------------------------"
	}
}

# UI Stop
function KeyDown {
	$WaitKeyDown = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}

# Client Execution Yes/No question
function YesNo {
	param(
		[parameter(Position=0, Mandatory=$false)]
		[string]$Title="Already Exists",
		[parameter(Position=1, Mandatory=$false)]
		[string]$Message="Do you want overwrite?"
	)
	
	$ChoiceYes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Answer Yes."
	$ChoiceNo = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Answer No."
	
	$Options = [System.Management.Automation.Host.ChoiceDescription[]]($ChoiceYes, $ChoiceNo)
	
	$Result = $Tabs + $host.ui.PromptForChoice($Title, $Message, $Options, 1)
	switch ($Result) {
		0 {
			return $true
		}
		1 {
			return $false
		}
	}
}

# Waiting for finish operation
function Waiting {
	param (
		[parameter(Position=0, Mandatory=$false)]
		[int]$TimeSleep=15,
		[parameter(Position=1, Mandatory=$false)]
		[int]$TimeOut		
	)
	if ($TimeOut -eq 0) {
		Write-Host -NoNewline "`nWorking on it.." -ForegroundColor "Cyan"	
	}
	Write-Host -NoNewline "."  -ForegroundColor "Cyan"	
	Start-Sleep -s $TimeSleep	
}

# IIsReset
function Reset {
	
	Write-Host "`nReseting IIS" -ForegroundColor "DarkMagenta"
	LogWrite "TRACE - Reseting IIS"
	iisreset /noforce
}

# Wsp file name presentation
function ShowHeader {
	param(
		[parameter(Position=0, Mandatory=$true)]
		$Item
	)
	
	$UnderLine = "="
	$UnderLine = $UnderLine * $Item.Name.Length
	Write-Host "`n`n`n$Item" -ForeGroundColor "White"
	Write-Host "`n$UnderLine" -ForeGroundColor "White"
}
